<?php
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/*
 * GEM/Crypt.php
 * 
 * This class provides cryptography features for
 * the GEM application.
 * 
 * @author David Joly
 * @link http://zeletron.com
 * 
 * @category GEM
 */
class GEM_Crypt
{
	/**
	 * Lower case source for hash generators
	 * 
	 * @var string
	 */
	public static $lcHashChars = "abcdefghijklmnopqrstuvwxyz";
	
	/**
	 * Upper case source for hash generators
	 * 
	 * @var string
	 */
	public static $ucHashChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	/**
	 * Digit source for hash generators
	 * 
	 * @var string
	 */
	public static $digitHashChars = "1234567890";
	
	/**
	 * Special character source for hash generators
	 * 
	 * @var string
	 */
	public static $specialHashChars = "!@#$%^&*(){}[]<>?";
	
	/**
	 * Generates a random salt value.
	 * 
	 * @param int $length
	 * @return string
	 */
	public static function generateSalt($length = 16){
		$hashChars 	= self::$digitHashChars
					. self::$lcHashChars
					. self::$ucHashChars;
		$salt = '';
		for($i = 0; $i < (int)$length; $i++){
     		$salt .= self::$hashChars[rand(0,strlen(self::$hashChars)-1)];
     	}
     	return $salt;
	}
	
	/**
	 * Generates a strong password containing:
	 * 		-Eight (8) characters
	 * 		-A lowercase letter
	 * 		-A uppercase letter
	 * 		-A digit
	 * 		-A special character
	 * 
	 * @return string
	 */
	public static function generatePassword(){
		$password 	= self::$digitHashChars[rand(0,strlen(self::$digitHashChars)- 1)]
					. self::$digitHashChars[rand(0,strlen(self::$digitHashChars) - 1)]
		 			. self::$lcHashChars[rand(0,strlen(self::$lcHashChars) - 1)]
		 			. self::$lcHashChars[rand(0,strlen(self::$lcHashChars) - 1)]
		 			. self::$lcHashChars[rand(0,strlen(self::$lcHashChars) - 1)]
		 			. self::$ucHashChars[rand(0,strlen(self::$ucHashChars) - 1)]
		 			. self::$ucHashChars[rand(0,strlen(self::$ucHashChars) - 1)]
		 			. self::$specialHashChars[rand(0,strlen(self::$specialHashChars) - 1)];
		 $password = str_shuffle($password);
		 return $password;
	}

}